//
// Created by hamlet on 2022/1/14.
//

#include "message.h"
#include "player.h"
#include "room.h"
#include "messageutil.h"

Message::Message(Message::Type type) : m_messageType(type) {
}

Message *Message::parseJson(const QJsonObject &json) {
    if (json.contains("type")) {
        switch (static_cast<Type>(json["type"].toInt())) {
            case Type::PlayerInfo:
                return PlayerInfo::parseJson(json);
            case Type::RoomInfo:
                return RoomInfo::parseJson(json);
            case Type::DeleteRoomMessage:
                return DeleteRoomMessage::parseJson(json);
            case Type::AllPlayerInfo:
                return AllPlayerInfo::parseJson(json);
            case Type::NewPlayerMessage:
                return NewPlayerMessage::parseJson(json);
            case Type::DeletePlayerMessage:
                return DeletePlayerMessage::parseJson(json);
            case Type::TextMessage:
                return TextMessage::parseJson(json);
            case Type::StartGameMessage:
                return StartGameMessage::parseJson(json);
            case Type::SnakeDirectionChangeMessage:
                return SnakeDirectionChangeMessage::parseJson(json);
            case Type::NewFruitMessage:
                return NewFruitMessage::parseJson(json);
            default:;
        }
    }
    return new UnknownMessage;
}
